package id.agung.wonogirimaps;

import id.agung.wonogirimaps.adapter.CustomAdapter;
import id.agung.wonogirimaps.entity.Lokasi;
import id.agung.wonogirimaps.util.JSONAdapter;
import id.agung.wonogirimaps.util.Utils;

import java.io.File;
import java.util.List;

import org.json.JSONObject;

import com.actionbarsherlock.app.SherlockActivity;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.utils.StorageUtils;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap.CompressFormat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;

public class LocationActivity extends SherlockActivity implements
		OnItemClickListener, TextWatcher
{
	private static final String			TAG			= LocationActivity.class
															.getName();

	public static final String			TAG_TITLE	= "title";
	public static final String			TAG_GAMBAR	= "img";
	public static final String			TAG_DESC	= "desc";
	public static final String			TAG_LAT		= "lat";
	public static final String			TAG_LNG		= "lng";

	private int							cat_id;

	private JSONAdapter					json;
	private ProgressDialog				pDialog;

	private CustomAdapter				adapter;

	private ListView					lv;
	private EditText					search;

	private ImageLoader					imageLoader;
	private ImageLoaderConfiguration	config;
	private DisplayImageOptions			options;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_location);

		getSupportActionBar().setTitle("Daftar Lokasi");
		getSupportActionBar().setIcon(android.R.color.transparent);


		File cacheDir = StorageUtils.getCacheDirectory(this);

		imageLoader = ImageLoader.getInstance();

		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_stub)
				.cacheOnDisc(true)
				.delayBeforeLoading(100)
				.cacheInMemory(true)
				.displayer(new RoundedBitmapDisplayer(20))
				.build();

		config = new ImageLoaderConfiguration.Builder(this)
				.discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
				.denyCacheImageMultipleSizesInMemory()
				.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
				.discCache(new UnlimitedDiscCache(cacheDir))
				.discCacheSize(50 * 1024 * 1024).discCacheFileCount(100)
				.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
				.writeDebugLogs().build();

		imageLoader.init(config);

		lv = (ListView) findViewById(R.id.list_loc);
		search = (EditText) findViewById(R.id.search_loc);

		// ketika data lokasi yang dipilih kosong
		lv.setEmptyView(findViewById(R.id.not_found_location));

		// memberikan event klik pada listview
		lv.setOnItemClickListener(this);

		// listener untuk pencarian
		// search.addTextChangedListener(this);

		Bundle b = getIntent().getExtras();

		if (b != null)
			cat_id = b.getInt(CategoryActivity.TAG_CAT);

		json = new JSONAdapter();

		new AsyncLoc().execute(Utils.API_LOCATION + cat_id);
	}

	private class AsyncLoc extends AsyncTask<String, Void, List<Lokasi>>
	{
		@Override
		protected List<Lokasi> doInBackground(String... arg0)
		{
			Utils.TRACE(TAG, Utils.API_LOCATION + cat_id);
			JSONObject obj = json.getJSONFromURL(arg0[0]);

			return json.getAllLocation(obj);
		}

		@Override
		protected void onPreExecute()
		{

			super.onPreExecute();

			pDialog = new ProgressDialog(LocationActivity.this);
			pDialog.setMessage("Loading");
			pDialog.setCancelable(true);
			pDialog.show();
		}

		@Override
		protected void onPostExecute(List<Lokasi> result)
		{

			super.onPostExecute(result);
			pDialog.dismiss();

			adapter = new CustomAdapter(LocationActivity.this, imageLoader,
					options, result);
			lv.setAdapter(adapter);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long id)
	{

		Bundle bundle = new Bundle();
		bundle.putString(TAG_TITLE,
				((Lokasi) adapter.getItem(position)).getNama());
		bundle.putString(TAG_DESC,
				((Lokasi) adapter.getItem(position)).getDeskripsi());
		bundle.putString(TAG_GAMBAR,
				((Lokasi) adapter.getItem(position)).getGambar());
		bundle.putDouble(TAG_LAT, ((Lokasi) adapter.getItem(position)).getLat());
		bundle.putDouble(TAG_LNG, ((Lokasi) adapter.getItem(position)).getLng());

		Intent i = new Intent(this, InfoLocationActivity.class);
		i.putExtras(bundle);
		startActivity(i);
		// finish();

	}

	@Override
	public void afterTextChanged(Editable arg0)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
	{
		// TODO Auto-generated method stub

	}

}
